package com.leet.code.simple_array;

/**
 * @description:
 * @author: WYG
 * @time: 2020/12/10 9:52
 */

public class CanThreePartsEqualSum {
    public boolean canThreePartsEqualSum(int[] A) {
        int sum = 0;
        for (int x : A) {
            sum += x;
        }
        if (sum % 3 != 0) {
            return false;
        }
        int avarage = sum / 3;
        int a = 1;
        int b = A.length - 2;
        int sum1 = A[0];
        int sum2 = A[A.length - 1];
        while (a < b + 1) {
            if (sum1 == avarage && sum2 == avarage) {
                return true;
            }
            if (sum1 != avarage) {
                sum1 += A[a++];
            }
            if (sum2 != avarage) {
                sum2 += A[b--];
            }
        }
        return false;
    }
    public boolean canThreePartsEqualSum1(int[] A) {
        int sum = 0;
        int num = 0;
        for (int x : A) {
            sum += x;
        }
        if (sum % 3 != 0) {
            return false;
        }
        int avarage = 0;
        for (int y : A) {
            avarage += y;
            if (avarage == sum / 3) {
                num++;
                avarage = 0;
            }
        }
        return num >= 3;
    }
}
